package com.sicnu.cheer.leaugebar.leaguemodle.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.sicnu.cheer.generalmodule.config.AppManager;
import com.sicnu.cheer.leaugebar.leaguemodle.R;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import butterknife.ButterKnife;
import cn.bmob.v3.Bmob;

/**
 * Created by cheer on 2017/2/22.
 */

public abstract class TabCommonActivity  extends AppCompatActivity implements View.OnClickListener {
    protected TabCommonActivity mThis;
    private Button leftTab;
    private Button rightTab;
    private List<Fragment> fragments;
    private List<String> titles;
    private FragmentManager manager;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getContentViewId());
        mThis = this;
        Bmob.initialize(this,getResources().getString(R.string.bmob_appkey));
        ButterKnife.inject(this);
        AppManager.getAppManager().addActivity(this);
        initView();
        initData();
        afterViews();
    }
    //初始化视图
    protected void initView(){
        ((TextView) findViewById(R.id.title)).setText(getTitleString());
        findViewById(R.id.back).setOnClickListener(this);
        leftTab = ((Button) findViewById(R.id.left_btn));
        leftTab.setOnClickListener(this);
        rightTab = ((Button) findViewById(R.id.right_btn));
        rightTab.setOnClickListener(this);
    }

    //初始化数据
    protected void initData(){
        manager = getSupportFragmentManager();
        titles=new ArrayList<>();
        fragments=new ArrayList<>();
        Map<String, Fragment> maps = getFragments();

        //设置标题和绑定fragment
        for (Map.Entry entry : maps.entrySet()) {
            titles.add ((String)entry.getKey());
            fragments.add((Fragment)entry.getValue());
        }

        FragmentTransaction trans=  manager.beginTransaction();
        trans.add(R.id.fragment_content, fragments.get(0));
        trans.add(R.id.fragment_content, fragments.get(1));
        trans.commit();

        setTabSelection(0);
        leftTab.setText(titles.get(0));
        rightTab.setText(titles.get(1));
    }

    //初始化完成之后
    protected void afterViews(){

    }

    public abstract int getContentViewId();

    public abstract String getTitleString();


    /**
     * 设置viewpager每个页面的标题和布局
     *
     * @return
     */
    protected abstract Map<String,Fragment> getFragments();

    @Override
    public void onClick(View v) {
        if (v.getId()==R.id.left_btn){
            setTabSelection(0);
        }else if (v.getId()==R.id.right_btn){
            setTabSelection(1);
        }else if (v.getId()==R.id.back){
            onBackPressed();
        }
    }

    private void setTabSelection(int i) {
        FragmentTransaction trans=  manager.beginTransaction();
        hideFragments(trans);
        leftTab.setSelected(i==0);
        rightTab.setSelected(i==1);
        trans.show(fragments.get(i));
        trans.commit();
    }

    private void hideFragments(FragmentTransaction trans) {
        for (Fragment fragment :
                fragments) {
            if (fragment!=null){
                trans.hide(fragment);
            }
        }
    }

}
